package com.senlingspace.system.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.senlingspace.common.enums.EqumentStatus;
import com.senlingspace.common.enums.TeamStatus;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.PageUtils;
import com.senlingspace.common.utils.StringUtils;
import com.senlingspace.system.domain.SlkTeamStatus;
import com.senlingspace.system.domain.SlkjOrderTracking;
import com.senlingspace.system.domain.dto.TeamStatusDto;
import com.senlingspace.system.domain.vo.SlkjTeamEqumentDetailVo;
import com.senlingspace.system.domain.vo.SlkjTeamEqumentVo;
import com.senlingspace.system.mapper.SlkjOrderTrackingMapper;
import com.senlingspace.system.service.ISlkjOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjTeamEqumentMapper;
import com.senlingspace.system.domain.SlkjTeamEqument;
import com.senlingspace.system.service.ISlkjTeamEqumentService;
import org.springframework.util.CollectionUtils;

/**
 * 设备组队关联Service业务层处理
 * 
 * @author SenlingSpace
 * @date 2024-08-21
 */
@Slf4j
@Service
public class SlkjTeamEqumentServiceImpl implements ISlkjTeamEqumentService 
{
    @Autowired
    private SlkjTeamEqumentMapper slkjTeamEqumentMapper;
    @Autowired
    private ISlkjOrderService slkjOrderService;
    @Autowired
    private  SlkjOrderTrackingMapper slkjOrderTrackingMapper;




    public List<SlkjTeamEqumentVo> getAggregatedTeamEqumentsByShopId(Long shopId) {

        // 查询聚合后的团队数据
        List<SlkjTeamEqumentVo> teamEqumentVos = slkjTeamEqumentMapper.selectAggregatedTeamEqumentsByShopId(shopId);
        if (teamEqumentVos == null) {
            return teamEqumentVos;
        }


        for (SlkjTeamEqumentVo teamEqumentVo : teamEqumentVos) {

            // 解析 members 字段，将其转换为详细信息列表
            List<SlkjTeamEqumentDetailVo> detailList = new ArrayList<>();
            if (StringUtils.isNotBlank(teamEqumentVo.getMembers())) {

                String[] members = teamEqumentVo.getMembers().split(";");

                for (String member : members) {
                    String[] parts = member.split(":");
                    SlkjTeamEqumentDetailVo detailVo = new SlkjTeamEqumentDetailVo();
                    if (StringUtils.isNotBlank(parts[0])) {
                        detailVo.setNikeName(parts[0]);
                    }

                    if (StringUtils.isNotBlank(parts[1])) {
                        detailVo.setUuid(parts[1]);
                    }

                    detailVo.setId(Long.valueOf(parts[2]));


                    if (StringUtils.isNotBlank(parts[3])) {
                        detailVo.setOrderNumber(parts[3]);
                    }

                    detailList.add(detailVo);
                }
                teamEqumentVo.setSlkjTeamEqumentDetailVos(detailList);

            }
        }

        return teamEqumentVos;
    }


    /**
     * 查询设备组队关联
     * 
     * @param id 设备组队关联主键
     * @return 设备组队关联
     */
    @Override
    public SlkjTeamEqument selectSlkjTeamEqumentById(Long id)
    {
        return slkjTeamEqumentMapper.selectSlkjTeamEqumentById(id);
    }

    /**
     * 查询设备组队关联列表
     * 
     * @param slkjTeamEqument 设备组队关联
     * @return 设备组队关联
     */
    @Override
    public List<SlkjTeamEqument> selectSlkjTeamEqumentList(SlkjTeamEqument slkjTeamEqument)
    {
        PageUtils.startPage();
        return slkjTeamEqumentMapper.selectSlkjTeamEqumentList(slkjTeamEqument);
    }

    /**
     * 新增设备组队关联
     * 
     * @param slkjTeamEqument 设备组队关联
     * @return 结果
     */
    @Override
    public int insertSlkjTeamEqument(SlkjTeamEqument slkjTeamEqument)
    {
        slkjTeamEqument.setCreateTime(DateUtils.getNowDate());
        return slkjTeamEqumentMapper.insertSlkjTeamEqument(slkjTeamEqument);
    }

    /**
     * 修改设备组队关联
     * 
     * @param slkjTeamEqument 设备组队关联
     * @return 结果
     */
    @Override
    public int updateSlkjTeamEqument(SlkjTeamEqument slkjTeamEqument)
    {
        slkjTeamEqument.setUpdateTime(DateUtils.getNowDate());
        return slkjTeamEqumentMapper.updateSlkjTeamEqument(slkjTeamEqument);
    }

    /**
     * 批量删除设备组队关联
     * 
     * @param ids 需要删除的设备组队关联主键
     * @return 结果
     */
    @Override
    public int deleteSlkjTeamEqumentByIds(Long[] ids)
    {
        return slkjTeamEqumentMapper.deleteSlkjTeamEqumentByIds(ids);
    }

    /**
     * 删除设备组队关联信息
     * 
     * @param id 设备组队关联主键
     * @return 结果
     */
    @Override
    public int deleteSlkjTeamEqumentById(Long id)
    {
        return slkjTeamEqumentMapper.deleteSlkjTeamEqumentById(id);
    }

    @Override
    public HashMap<String, List> listByIsEqument(Long shopId) {

        HashMap<String, List> greapEquStatus = new HashMap<>();

//        查询状态为未领取的组队列表信息
        SlkjTeamEqument slkjTeamEqument = new SlkjTeamEqument();
        slkjTeamEqument.setShopId(shopId);
        slkjTeamEqument.setStatus(EqumentStatus.CREATE.getCode());
        slkjTeamEqument.setIsEqument(0);
        List<HashMap<String,String>>  greapNoEqu =  slkjTeamEqumentMapper.listByIsEqument(slkjTeamEqument);
        if (!CollectionUtils.isEmpty(greapNoEqu) && StringUtils.isNotBlank( greapNoEqu.get(0).get("ids") ) ) {
            greapEquStatus.put("greapNoEqu",greapNoEqu);
        }


        //        查询状态为已领取的组队列表信息
        slkjTeamEqument.setStatus(null);
        slkjTeamEqument.setIsEqument(1);
        List<HashMap<String,String>>  greapYesEqu =  slkjTeamEqumentMapper.listByIsEqument(slkjTeamEqument);
        if (!CollectionUtils.isEmpty(greapYesEqu) && StringUtils.isNotBlank( greapYesEqu.get(0).get("ids") ) ) {
            greapEquStatus.put("greapYesEqu",greapYesEqu);
        }

        return greapEquStatus;

    }

    @Override
    public int updateByIsEqument(long[] idArray, Integer isEqument) {

        return slkjTeamEqumentMapper.updateByIsEqument(idArray,isEqument);
    }
}
